#include <vector>
#include <iostream>
#include <string>
#include <limits>
#include <algorithm>

using namespace std;

int matchPi(vector<string> list, string str)
{
  int result = INT_MAX;
  if(find(list.begin(), list.end(), str) != list.end())
  {
    return 0;
  }
  vector<int> results;
  for(int i = 0; i < str.size(); ++i)
  {
    string before;
    for(int j = 0; j < i; ++j)
    {
      before.push_back(str[j]);
    }
    string next;
    for(int j = i; j < str.size(); ++j)
    {
      next.push_back(str[j]);
    }
    if(find(list.begin(), list.end(), before) != list.end())
    {
      result = min(matchPi(list, next), result);
    }
  }
  return result + 1;
}

int main(int argc, char const *argv[])
{
  vector<string> list{"3141", "5", "31", "2", "4159", "9", "42"};
  string Pi{"3141592"};
  cout << matchPi(list, Pi) << endl;
  return 0;
}
